package com.mbigger.admin.common;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * 返回结果的统一封装
 * 
 * @author mbigg
 *
 */
@Getter
@Setter
public class JsonResultHoler {
	private boolean success;
	private Object data;
	private String msg;

	private JsonResultHoler(boolean success) {
		this.success = success;
	}

	public static JsonResultHoler success() {
		return new JsonResultHoler(true);
	}

	public static JsonResultHoler success(Object data) {
		JsonResultHoler holder = new JsonResultHoler(true);
		holder.setData(data);
		return holder;
	}

	public static JsonResultHoler fail(String msg) {
		JsonResultHoler holder = new JsonResultHoler(false);
		holder.setMsg(msg);
		return holder;
	}

	public Map<String, Object> toMap() {
		HashMap<String, Object> result = new HashMap<String, Object>(30, (float) 0.75);
		result.put("success", success);
		if (data != null) {
			result.put("data", data);
		}
		if (!StringUtils.isEmpty(msg)) {
			result.put("msg", msg);
		}
		return result;
	}
}
